package top.v5it.japi.ddd.example.points.service.imple;

import cn.hutool.core.lang.Assert;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import top.v5it.japi.ddd.core.dao.impl.BasicDaoJdbcImpl;
import top.v5it.japi.ddd.core.service.impl.BasicServiceImpl;
import top.v5it.japi.ddd.example.points.entity.PointsRecord;
import top.v5it.japi.ddd.example.points.service.PointsRecordService;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author zhanpu
 * @date 2023/11/10
 */
@Service(PointsRecordService.SERVICE_NAME)
@RequiredArgsConstructor
public class PointsRecordServiceImpl extends BasicServiceImpl<BasicDaoJdbcImpl> implements PointsRecordService {

    private final RestTemplate restTemplate;

    @Override
    public void add(PointsRecord entity) {
        Assert.notNull(entity, "请求参数[entity]不能为空");
        final Object result = restTemplate.getForObject("http://example-member/orm/member/search_by_id?id=" + entity.getMember_id(), Object.class);
        Assert.notNull(result, "请求参数[order.member_id = {}]非法", entity.getMember_id());
        Optional.of(entity).filter(t -> Objects.isNull(t.getRecord_date())).ifPresent(t -> t.setRecord_date(LocalDateTime.now(ZoneId.systemDefault())));
        basicDao.insert(entity);
    }

    @Override
    public List<PointsRecord> searchAllByMemberId(Long memberId) {
        Assert.notNull(memberId, "请求id不能为空");
        final PointsRecord pointsRecord = new PointsRecord();
        pointsRecord.setMember_id(memberId);
        return basicDao.loadAll(pointsRecord);
    }
}
